//
//  LBFTravelViewController.m
//  Go!
//
//  Created by 凌風 on 16/7/7.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "LBFTravelViewController.h"
#import "WAdvertScrollView.h"
#import "WClassCollectionViewCell.h"
#import "WShopTableViewCell.h"
#import "NetWorkManager+Travel.h"
#import "WHandleData.h"
#import "WShopModel.h"
#import "W_DetailViewController.h"

#import "HQYFoodKindViewController.h" // 点击collectionview跳转页面

#define ClassCell_ID @"classCellID"
#define ShopCell_ID @"shopCellID"
#define CellHeight Size_H(100)
#define HeaderHeight Size_H(50)
@interface LBFTravelViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

#pragma mark -- Views
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIScrollView *scrollView;/**< 全屏滚动视图 */
@property (nonatomic, strong) WAdvertScrollView *topAdvertView; /**< 顶部轮播广告 */
@property (nonatomic, strong) UICollectionView *classificationCollectionView;/**< 中部分类按钮 */
@property (nonatomic, strong) UITableView *shopTableView;/**< 店铺TableView */
@property (nonatomic, strong) UIView *customNavBar; /**< 自定义navigationBar */
@property (nonatomic, strong) UIView *headerView; /**< tableView头部视图 */
@property (nonatomic, strong) UISegmentedControl *segmentControl;/**< “国内游”、“境外游”分段控制器 */

#pragma mark -- Data
@property (nonatomic, strong) NSMutableArray *topAdverts;/**< 顶部广告图片数组 */
@property (nonatomic, strong) NSArray *classTitle; /**< 分类按钮标题数组 */
@property (nonatomic, strong) WHandleData *shopData; /**< 店铺数据源处理 */
@property (nonatomic, strong) NSArray *shopDataSource; /**< 店铺列表数据源 */
@property (nonatomic, strong) NSDictionary *shopDetailInfo; /**< 店铺详细信息 */

#pragma mark -- Controller 
@property (nonatomic, strong) W_DetailViewController *detailVc;/**< 商户详情控制器 */

@end

@implementation LBFTravelViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationItem.title = @"旅游";
    // Do any additional setup after loading the view.
    
    /*
    
    // 配置网络请求
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://lvyou.baidu.com/static/foreign/page/ticket/channel/channel.html?from=webapp"]];
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
    
    */
    [self initializeDataSource];
    [self initializeUserInterface];
    
}

/**< 初始化数据源 */
- (void)initializeDataSource {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(respondsToNotification:) name:@"shopsDataSource" object:nil];
    
//    self.shopData = [[WHandleData alloc] init];
    [self.shopData getShopsDataSourceWithSubcatId:@"947"];
    
}
/**< 初始化用户界面 */
- (void)initializeUserInterface {
    // 自定义navigationBar
//    [self.view addSubview:self.customNavBar];
    
    self.navigationItem.title = @"旅游";
    // 取消导航栏的自适应
    self.automaticallyAdjustsScrollViewInsets = false;
    
    // 全屏滚动视图
    [self.view addSubview:self.scrollView];
    
    // 顶部广告栏
    self.topAdverts = [@[@"广告1.jpg"] mutableCopy];
    [self.scrollView addSubview:self.topAdvertView];
    
    // 中间分类按钮
    [self.scrollView addSubview:self.classificationCollectionView];
    
    // 底部店铺
//    [self.scrollView addSubview:self.shopTableView];
    
    
//    self.scrollView.contentSize = CGSizeMake(ScreenW, CGRectGetMaxY(self.shopTableView.frame));
    
    // 店铺头部视图
    self.headerView.frame = CGRectMake(0, CGRectGetMaxY(self.classificationCollectionView.frame) + 1, ScreenW, HeaderHeight);
    [self.scrollView addSubview:self.headerView];
    [self.scrollView sendSubviewToBack:self.headerView];
    
    [self.view bringSubviewToFront:self.customNavBar];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = false;

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    self.navigationController.navigationBar.hidden = false;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- UICollectionViewDataSource, UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.classTitle.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    WClassCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ClassCell_ID forIndexPath:indexPath];
    
    cell.cellTitle = self.classTitle[indexPath.item];
    cell.imageName = self.classTitle[indexPath.item];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HQYFoodKindViewController *nextVc = [[HQYFoodKindViewController alloc] init];
    
    if (indexPath.item == 0) {
        nextVc.classId = @"320";
        nextVc.foodId = @"688";
    } else if (indexPath.item == 1) {
        nextVc.classId = @"320";
        nextVc.foodId = @"920";
    } else if (indexPath.item == 2) {
        nextVc.classId = @"320";
        nextVc.foodId = @"338";
    } else if (indexPath.item == 3) {
        nextVc.classId = @"320";
        nextVc.foodId = @"953";
    } else if (indexPath.item == 4) {
        nextVc.classId = @"320";
        nextVc.foodId = @"914";
    } else if (indexPath.item == 5) {
        nextVc.classId = @"320";
        nextVc.foodId = @"700";
    } else if (indexPath.item == 6) {
        nextVc.classId = @"316";
        nextVc.foodId = @"702";
    } else if (indexPath.item == 7) {
        nextVc.classId = @"320";
        nextVc.foodId = @"528";
    }
    
    nextVc.title = self.classTitle[indexPath.item];
    
    [self.navigationController pushViewController:nextVc animated:true];
    NSLog(@"点击了->%@", self.classTitle[indexPath.item]);
    
}


#pragma mark -- UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.shopDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ShopCell_ID];
//    cell.backgroundColor = [UIColor lightGrayColor];
    cell.model = self.shopDataSource[indexPath.row];
//    NSLog(@"%@", self.shopDataSource[indexPath.row]);
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self pushToDetailViewControllerWithIndexPath:indexPath];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return CellHeight;
}

#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < CGRectGetMaxY(self.classificationCollectionView.frame) - 64) {
        self.shopTableView.scrollEnabled = false;
        self.headerView.frame = CGRectMake(0, CGRectGetMaxY(self.classificationCollectionView.frame) + 1, ScreenW, HeaderHeight);
        [scrollView addSubview:self.headerView];
        [scrollView sendSubviewToBack:self.headerView];
    } else {
        
        self.headerView.frame = CGRectMake(0, 64, ScreenW, HeaderHeight);
        [self.view addSubview:self.headerView];
        
        self.shopTableView.scrollEnabled = true;
    }
}

#pragma mark -- 监听事件的回调方法
- (void)respondsToNotification:(NSNotification *)notif {
    
    if ([notif.userInfo[@"user"] isEqualToString:@"shopsTableView"]) {
        
        self.shopDataSource = notif.object;

        [self.scrollView addSubview:self.shopTableView];
        self.shopTableView.frame = CGRectMake(0, CGRectGetMaxY(self.classificationCollectionView.frame) + HeaderHeight + 1, ScreenW, CellHeight * self.shopDataSource.count);
        [self.shopTableView reloadData];
        
        self.scrollView.contentSize = CGSizeMake(ScreenW, CGRectGetMaxY(self.shopTableView.frame));
        
    } else if ([notif.userInfo[@"user"] isEqualToString:@"shopDetailInfo"]) {
        
        self.shopDetailInfo = notif.object;
        
        self.detailVc.detailInfo = self.shopDetailInfo;
        

//        NSLog(@"->%@", self.detailVc.detailInfo);
    }
    
}

- (void)respondsToSegmentControl:(UISegmentedControl *)segment {
    
    if (segment.selectedSegmentIndex == 0) {
        [self.shopTableView removeFromSuperview];
        [self.shopData getShopsDataSourceWithSubcatId:@"947"];
    } else if (segment.selectedSegmentIndex == 1) {
        [self.shopTableView removeFromSuperview];
        [self.shopData getShopsDataSourceWithSubcatId:@"948"];
    }
    
}


#pragma mark -- 私有方法
- (void)pushToDetailViewControllerWithIndexPath:(NSIndexPath *)indexPath {
    WShopModel *shopModel = self.shopDataSource[indexPath.row];
    
    NSString *shopId = shopModel.shop_Id ;
    
    [self.shopData getShopDetailInfoWithShopID:shopId];
    
    self.detailVc = [[W_DetailViewController alloc] init];
    self.detailVc.imageURL = shopModel.imageURL;
    
    [self.navigationController pushViewController:self.detailVc animated:true];
 
}

#pragma mark -- Getter
- (UIWebView *)webView {
    if (!_webView) {
        _webView = [[UIWebView alloc] init];
        _webView.frame = CGRectMake(0, Size_H(-50), ScreenW, ScreenH);
        UIView *maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, Size_H(50))];
        maskView.backgroundColor = [UIColor greenColor];
        [_webView addSubview:maskView];
    }
    
    return _webView;
}

- (UIView *)customNavBar {
    if (!_customNavBar) {
        _customNavBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 64)];
        _customNavBar.backgroundColor = [UIColor whiteColor];
        
        UILabel_Font *label = [[UILabel_Font alloc] init];
        label.frame = CGRectMake((ScreenW - 100) / 2, 20, 100, 44);

        label.text = @"旅游";
        label.font = [UIFont systemFontOfSize:Font(20)];
        label.textAlignment = NSTextAlignmentCenter;
        
        [_customNavBar addSubview:label];
    }
    
    return _customNavBar;
}

- (NSArray *)classTitle {
    if (!_classTitle) {
        _classTitle = @[@"景点郊游", @"游乐园", @"温泉洗浴", @"展览馆", @"滑雪", @"采摘", @"亲子", @"其它"];
    }
    
    return _classTitle;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - 49)];
        
        _scrollView.backgroundColor = [UIColor whiteColor];
        
        _scrollView.delegate = self;
    }
    
    return _scrollView;
}

- (WAdvertScrollView *)topAdvertView {
    if (!_topAdvertView) {
        _topAdvertView = [[WAdvertScrollView alloc] initWithFrame:CGRectMake(0, 64, ScreenW, Size_H(100)) ImageArray:self.topAdverts];
        _topAdvertView.backgroundColor = [UIColor whiteColor];
    }

    
    return _topAdvertView;
}

- (UICollectionView *)classificationCollectionView {
    if (!_classificationCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        layout.itemSize = CGSizeMake(ScreenW / 4.0, ScreenW / 4.0);
        
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        
        _classificationCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.topAdvertView.frame), ScreenW, ScreenW / 4.0 * 2) collectionViewLayout:layout];
        _classificationCollectionView.backgroundColor = [UIColor whiteColor];
        
        _classificationCollectionView.dataSource = self;
        _classificationCollectionView.delegate = self;
        
        [_classificationCollectionView registerClass:[WClassCollectionViewCell class] forCellWithReuseIdentifier:ClassCell_ID];
    }
    
    return _classificationCollectionView;
}

- (UITableView *)shopTableView {
    if (!_shopTableView) {
        _shopTableView = [[UITableView alloc] init];
        
        _shopTableView.dataSource = self;
        _shopTableView.delegate = self;
        _shopTableView.scrollEnabled = false;
        
        _shopTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        [_shopTableView registerClass:[WShopTableViewCell class] forCellReuseIdentifier:ShopCell_ID];
    }
    
    return _shopTableView;
}

- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] init];
//        _headerView.backgroundColor = [UIColor redColor];
        _headerView.layer.masksToBounds = true;
        _headerView.layer.cornerRadius = 4;
        [_headerView addSubview:self.segmentControl];
    }
    
    return _headerView;
}

- (UISegmentedControl *)segmentControl {
    if (!_segmentControl) {
        NSArray *items = @[@"国内游", @"境外游"];
        _segmentControl = [[UISegmentedControl alloc] initWithItems:items];
        _segmentControl.selectedSegmentIndex = 0;
        _segmentControl.frame = CGRectMake(0, 0, ScreenW, HeaderHeight);
        _segmentControl.backgroundColor = [UIColor whiteColor];
        _segmentControl.tintColor = ColorWith(17, 201, 87);
        
        [_segmentControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:Font(20)], NSForegroundColorAttributeName:ColorWith(17, 201, 87)} forState:UIControlStateNormal];
        [_segmentControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateSelected];
        
        [_segmentControl addTarget:self action:@selector(respondsToSegmentControl:) forControlEvents:UIControlEventValueChanged];
    }
    
    return _segmentControl;
}


#pragma mark -- 数据源
- (WHandleData *)shopData {
    if (!_shopData) {
        _shopData = [[WHandleData alloc] init];
    }
    
    return _shopData;
}


@end
